// pages/maker/makerdetails/index.js
const app = getApp() // 引入app
import _request from '../../../utils/request.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    indicatorDots: true,
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 500,
    imgUrl: app.globalData.imgUrl, //全局img路径
    equitiesList: [{
        id: 0,
        name: '订单收益奖',
        img: 'maker-eq1.png',
        bool: false,
        imgno: 'maker-no-eq1.png',
      },
      {
        id: 1,
        name: '直推创客奖',
        img: 'maker-eq2.png',
        bool: false,
        imgno: 'maker-no-eq2.png',
      },
      {
        id: 2,
        name: '推荐美发师',
        img: 'maker-eq3.png',
        bool: false,
        imgno: 'maker-no-eq3.png',
      },
      {
        id: 3,
        name: '推荐门店奖',
        img: 'maker-eq4.png',
        bool: false,
        imgno: 'maker-no-eq4.png',
      },
      {
        id: 4,
        name: '推荐新人奖',
        img: 'maker-eq5.png',
        bool: false,
        imgno: 'maker-no-eq5.png',
      },
      {
        id: 5,
        name: '分享商品收益',
        img: 'maker-eq6.png',
        bool: false,
        imgno: 'maker-no-eq6.png',
      },
      {
        id: 6,
        name: '门店股东',
        img: 'maker-eq7.png',
        bool: false,
        imgno: 'maker-no-eq7.png',
      },
      {
        id: 7,
        name: '间推创客奖',
        img: 'maker-eq8.png',
        bool: false,
        imgno: 'maker-no-eq8.png',
      },
      {
        id: 8,
        name: '人才储备',
        img: 'maker-eq9.png',
        bool: false,
        imgno: 'maker-no-eq9.png',
      },
      {
        id: 9,
        name: '全球分红',
        img: 'maker-eq10.png',
        bool: false,
        imgno: 'maker-no-eq10.png',
      },
    ],
    eqshow: 6,
    moneyList: ['10', '15', '20', '25', '30'],
    shareList: [{
        title: '推荐门店',
        common: '推荐线下店家入驻平台'
      },
      {
        title: '推荐美发师',
        common: '推荐优秀美发师入驻平台'
      },
      {
        title: '推荐用户',
        common: '推荐亲朋好友使用意约服务'
      }
    ],
    ProductList: [],
    current: 0
  },

  // 前往收益
  gomakerincome() {
    wx.navigateTo({
      url: '/pages/myincome/makerincome/index',
    })
  },

  // 点击展示更多权限
  eqShowmore() {
    this.setData({
      eqshow: 10
    })
  },

  // 点击展示更少权限
  eqShowmuch() {
    this.setData({
      eqshow: 6
    })
  },

  // 轮播
  changeIndicatorDots() {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },

  changeAutoplay() {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },

  intervalChange(e) {
    this.setData({
      interval: e.detail.value
    })
  },


  durationChange(e) {
    this.setData({
      duration: e.detail.value
    })
  },

  // 划动解锁对应等级
  swiperChange(e) {
    let current = Number(e.detail.current)
    let list = this.data.makerDetail.makerUpRule[current]
    this.setData({
      equitiesList: this.data.equitiesList.map((item, index) => {
        if (list.activatedBenefits.indexOf(item.name) < 0) {
          item.bool = false
        } else {
          item.bool = true
        }
        return item
      })
    })

  },

  // 购买
  onBuy() {
    wx.navigateTo({
      url: '/pages/maker/buymaker/pay/index',
    })
  },

  // 前往分享
  onShare() {
    wx.navigateTo({
      url: '/pages/generalize/index',
    })
  },

  // 前往发红包
  onRedenvelope(e) {
    const {
      type
    } = e.currentTarget.dataset
    // 跳转页面的tans下标
    app.globalData.redenvelopeactive = type
    wx.navigateTo({
      url: '/pages/redenvelope/index',
    })
  },

  // 查看规则
  onRlue() {
    wx.navigateTo({
      url: '/pages/maker/makerdetails/makerrule/index',
    })
  },

  // 查看权益
  onRleuDetails(e) {
    const {
      id
    } = e.currentTarget.dataset
    wx.navigateTo({
      url: '/pages/maker/makerequities/index?id=' + id,
    })
  },

  // 获取赠送创客卡的商品套餐列表
  getProductPackageInfo() {
    _request.postObj('/mallProduct/getMakerMallProductList', {
      pageNo: 1,
      pageSize: 4,
      userId: wx.getStorageSync('userid')
    }).then((res) => {
      this.setData({
        ProductList: res.data.data
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 获取用户创客信息
    let vm = this
    _request.get('/maker/userMakerInfo', {
      userId: wx.getStorageSync('userid')
    }).then((res) => {
      if (res.data.code == 200) {
        let data = res.data.data
        let indexs
        let makerUpRule = data.makerUpRule.map((item, index) => {
          // 选中当前等级
          if(item.description == data.description){
            indexs = index
            this.setData({
              current: index
            })
          }
          // 获取下一次所需要的人数
          if (index != 4) {
            item.nextlevel = data.makerUpRule[index + 1].upgradeNum
            item.nextdescription = data.makerUpRule[index + 1].description
            // 计算当前升级所需百分比
            item.percentage = data.downBuySum / (item.nextlevel / 100)
          }
        })
        vm.setData({
          makerDetail: {
            makerUpRule,
            ...data
          },
          equitiesList: vm.data.equitiesList.map(item => {
            console.log(indexs,'indexs');
            console.log(data.makerUpRule[indexs],'itemss');
            // 当前等级图标高亮activatedBenefits
            if (data.makerUpRule[indexs].activatedBenefits.indexOf(item.name) < 0) {
              item.bool = false
            } else {
              item.bool = true
            }
            return item
          })
        })
        console.log(vm.data.makerDetail)
      }
    }).catch((error) => {
      console.log(error, 'error');
    })

    this.getProductPackageInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.reLaunch({
      url: '/firstpages/my/index',
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})